<?php

namespace Aria;

class JsonRPC
{
    protected $url = '';
    protected $urls = array();
    protected $savePath = '';
    protected $saveName = '';
    protected $callName = '';
    protected $args = array();
    protected $ch;
    protected $token;

    function __construct($server = 'http://127.0.0.1:6800/jsonrpc', $token = null)
    {
        $this->ch = curl_init($server);
        curl_setopt_array($this->ch, [
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false
        ]);
        if (!is_null($token)) {
            $this->token = $token;
        }
    }

    /**
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * 添加一个下载地址
     * @param string $url
     * @return JsonRPC
     */
    public function setUrl($url)
    {
        $this->url = $url;
        $this->urls[] = $url;
        $this->callName = 'addUri';
        return $this;
    }

    /**
     * @return array
     */
    public function getUrls()
    {
        return $this->urls;
    }

    /**
     * 添加多源文件，用于当一个源下载失败时自动换源
     * @param array $urls
     * @return JsonRPC
     */
    public function setUrls($urls)
    {
        $this->urls = $urls;
        $this->callName = 'addUri';
        return $this;
    }

    /**
     * @return string
     */
    public function getSavePath()
    {
        return $this->savePath;
    }

    /**
     * @param string $savePath
     * @return JsonRPC
     */
    public function setSavePath($savePath)
    {
        $this->savePath = $savePath;
        return $this;
    }

    /**
     * @return string
     */
    public function getSaveName()
    {
        return $this->saveName;
    }

    /**
     * @param string $saveName
     * @return JsonRPC
     */
    public function setSaveName($saveName)
    {
        $this->saveName = $saveName;
        return $this;
    }

    /**
     * @return array
     */
    public function getArgs()
    {
        return $this->args;
    }

    /**
     * 自定义参数
     * @param array $args
     * @return JsonRPC
     */
    public function setArgs($args)
    {
        $this->args[1] = $args;
        return $this;
    }

    /**
     *发出调用请求
     */
    public function call()
    {
        $this->args[0] = $this->urls;
        if (!empty($this->saveName)) {
            if (is_null($this->args[1])) {
                $this->args[1] = ['out' => $this->saveName];
            } else {
                $this->args[1] = array_merge([
                    'out' => $this->saveName
                ], $this->args[1]);
            }
        }
        if (!empty($this->savePath)) {
            if (is_null($this->args[1])) {
                $this->args[1] = ['dir' => $this->savePath];
            } else {
                $this->args[1] = array_merge([
                    'dir' => $this->savePath
                ], $this->args[1]);
            }
        }
        return $this->__call($this->callName, $this->args);
    }

    /**
     * 魔术方法调用自定义rpc接口
     * @param $name
     * @param $arg
     * @return mixed
     */
    function __call($name, $arg)
    {
        if (!is_null($this->token)) {
            array_unshift($arg, 'token:' . $this->token);
        }
        $data = [
            'jsonrpc' => '2.0',
            'id' => '1',
            'method' => 'aria2.' . $name,
            'params' => $arg
        ];
        $data = json_encode($data);
        print_r($data);
        echo "\n";
        $response = $this->req($data);
        if($response===false) {
            trigger_error(curl_error($this->ch));
        }
        $this->clean();
        return json_decode($response, 1);
    }


    /**
     * curl请求函数
     * @param $data
     * @return bool|string
     */
    protected function req($data)
    {
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
        return curl_exec($this->ch);
    }

    /**
     *析构方法释放资源
     */
    function __destruct()
    {
        curl_close($this->ch);
    }

    protected function clean()
    {
        if (!is_null($this->args[1])){
            if (array_search($this->saveName,$this->args[1])){
                unset($this->args[1]['out']);
            }
        }
        $this->savePath='';
        $this->saveName='';
        $this->urls=array();
        $this->url='';
    }


}
